<template>
  <div>
    <el-row :gutter="20">
      <el-col :lg="22" :md="16" :sm="24" :xl="22" :xs="24">
        <vab-card shadow="hover">
          <vab-query-form>
            <vab-query-form-top-panel :span="12">
              <el-button
                v-if="MenuAuth('add')"
                :icon="Plus"
                type="primary"
                @click="handleEdit()"
              >
                添加
              </el-button>
              <el-form inline :model="queryForm" @submit.prevent>
                <el-form-item>
                  <el-input
                    v-model.trim="queryForm.search"
                    clearable
                    placeholder="请输入名称"
                  />
                </el-form-item>
                <el-form-item>
                  <el-button
                    :icon="Search"
                    type="primary"
                    @click="fetchData('search')"
                  >
                    查询
                  </el-button>
                </el-form-item>
              </el-form>
            </vab-query-form-top-panel>
          </vab-query-form>
          <el-table
            v-loading="listLoading"
            border
            :data="list"
            default-expand-all
            min-width="1600px"
            row-key="path"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          >
            <!-- <el-table-column label="排序号" prop="SortCode " show-overflow-tooltip /> -->
            <el-table-column
              label="容器编码"
              min-width="92"
              prop="Code"
              show-overflow-tooltip
            />
            <el-table-column
              label="容器名称"
              min-width="92"
              prop="FullName"
              show-overflow-tooltip
            />
            <el-table-column
              label="容器类型"
              min-width="92"
              prop="CType"
              show-overflow-tooltip
            />
            <el-table-column
              label="重量上限"
              min-width="86"
              prop="WeightLimit"
              show-overflow-tooltip
            />
            <el-table-column
              label="体积上限"
              min-width="86"
              prop="VolumeLimit "
              show-overflow-tooltip
            />
            <el-table-column
              label="数量上限"
              min-width="86"
              prop="QuantityLimit"
              show-overflow-tooltip
            />
            <el-table-column
              label="创建人"
              min-width="86"
              prop="CreateUserName "
              show-overflow-tooltip
            />
            <el-table-column
              label="创建时间"
              min-width="112"
              prop="CreateDate "
              show-overflow-tooltip
            />
            <!-- <el-table-column
                    label="仓库地址"
                    prop="Address"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="详细地址"
                    prop="AddressDetail"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="邮编"
                    prop="PostCode"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    label="创建人"
                    prop="CreateUserName "
                    show-overflow-tooltip
                  /> -->
            <el-table-column
              label="是否有效"
              min-width="92"
              show-overflow-tooltip
            >
              <template #default="{ row }">
                {{ row.Enabled ? '是' : '否' }}
              </template>
            </el-table-column>
            <el-table-column label="备注" prop="Remark" show-overflow-tooltip />
            <el-table-column
              align="center"
              label="操作"
              show-overflow-tooltip
              width="180"
            >
              <template #default="{ row }">
                <el-button
                  v-if="MenuAuth('edit')"
                  text
                  type="primary"
                  @click="handleEdit(row, 'edit')"
                >
                  编辑
                </el-button>
                <el-button
                  v-if="MenuAuth('delete')"
                  text
                  type="danger"
                  @click="handleDelete(row)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
            <template #empty>
              <!-- <el-image
                      class="vab-data-empty"
                      :src="require('@/assets/empty_images/data_empty.png')"
                    /> -->
              <el-empty class="vab-data-empty" description="暂无数据" />
            </template>
          </el-table>
          <el-pagination
            background
            :current-page="queryForm.pageNo"
            :layout="layout"
            :page-size="queryForm.pageSize"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </vab-card>
      </el-col>
    </el-row>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, getList, getData } from '@/api/container'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'
  import { useSettingsStore } from '@/store/modules/settings'
  import { MenuAuth } from '~/src/utils/permission'
  export default defineComponent({
    name: 'Container',
    components: {
      Edit: defineAsyncComponent(() => import('./components/ContainerEdit')),
    },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        editRef: null,
        list: [],
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          wheres: `[{"name":"ModuleId","value":"value","displayType":"equal"}]`,
          export: false,
          moduleType: 'ModuleId',
          sort: 'SortCode',
          order: 'ASC',
          search: '',
        },
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        listLoading: true,
        expandDefault: [],
      })
      //列表编辑
      const handleEdit = (row, type) => {
        if (row && row.SID) {
          state['editRef'].showEdit(row, type)
        } else {
          // if (!row) state['editRef'].showEdit(state.queryForm)
          state['editRef'].showEdit(row, type)
        }
      }
      //页面刷新
      const settingsStore = useSettingsStore()
      const { theme } = storeToRefs(settingsStore)
      const $pub = inject('$pub')
      const refreshRoute = () => {
        $pub('reload-router-view')
      }
      //列表删除
      const handleDelete = (row) => {
        if (row.CID) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { message } = await doDelete({
              cid: row.CID,
            })
            //删除列表默认回到第一页
            $baseMessage(message, 'success', 'vab-hey-message-success')
            // refreshRoute()
            fetchData('search')
          })
        }
      }
      //菜单表格
      const fetchData = async (val) => {
        //wheres重新赋值
        const params = JSON.parse(JSON.stringify(state.queryForm))
        //搜索
        if (val == 'search') {
          params.pageNo = 1
          params.wheres = `[{"name":"FullName","value":"${params.search}","displayType":"like"}]`
        } else {
          params.wheres = `[{"name":"FullName","value":"","displayType":"equal"}]`
        }
        state.listLoading = true
        const {
          data: { rows, total },
        } = await getList(params)
        state.list = rows
        state.total = total
        state.listLoading = false
      }
      //分页条数切换
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      //分页切换
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        handleEdit,
        handleDelete,
        fetchData,
        Plus,
        handleCurrentChange,
        handleSizeChange,
        refreshRoute,
        MenuAuth,
      }
    },
  })
</script>
